﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using LoomDataEmulator.Data;

namespace LoomDataEmulator.TestSuite.TestHelpers
{
    public class SimpleFileCreatorHelper
    {
        private string _filePath;
        private FileInfo _fileInfo;
        private FileMetaData _fileMetaData;

        public FileMetaData FileMetaData
        {
            get { return _fileMetaData; }
            set { _fileMetaData = value; }
        }

        public FileInfo FileInfo
        {
            get { return _fileInfo; }
            set { _fileInfo = value; }
        }

        public SimpleFileCreatorHelper()
        {
        }

        public SimpleFileCreatorHelper(string Path)
        {
            this.CreateTestFile(Path);
            _fileInfo = new FileInfo(Path);
            _filePath = Path;
        }

        public void CreateTestFile(string Path)
        {
            StreamWriter writer = new StreamWriter(Path);
            writer.Write("test");

            writer.Flush();
            writer.Close();
            writer.Dispose();

            _fileInfo = new FileInfo(Path);
            _filePath = Path;
        }

        public void DeleteTestFile()
        {
            if (_fileInfo != null)
                _fileInfo.Delete();
        }

        public void CreateFileMetaData()
        {
            FileMetaData = new FileMetaData();
            FileMetaData.Table = new Table();

            var field1 = new Field();
            field1.Name = "ID";
            field1.Value = 1;
            field1.FieldType = FieldType.INTEGER;

            var field2 = new Field();
            field2.Name = "Description";
            field2.Value = "Field description";
            field2.FieldType = FieldType.VARCHAR;

            FileMetaData.Table.Fields.Add(field1);
            FileMetaData.Table.Fields.Add(field2);

            if (_fileInfo == null)
            {
                CreateTestFile(string.Format("C:\\TEMP\\{0}", new Guid()));
            }
            else
            {
                FileMetaData.FileInfo = _fileInfo;
            }
        }
    }
}
